package com.mifengs.mall.floor;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.BannerBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.loader.GlideImageLoader;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

/**
 * Created by lihaoqi on 2018/4/19.
 */


@ItemProviderTag(
        viewType = FloorListAdapter.TYPE_THIRTEEN,
        layout = R.layout.floor_big_banner
)
public class FloorThirteenProvider extends BaseItemProvider<FloorBean,BaseViewHolder> {


    @Override
    public void convert(BaseViewHolder helper, FloorBean floorBean, int position) {
        ImageView img = helper.getView(R.id.img);
        Banner banner = helper.getView(R.id.banner);
        final List<BannerBean> bannerBeanList = floorBean.getBanners();
        if (bannerBeanList.size() == 1) {
            banner.setVisibility(View.GONE);
            img.setVisibility(View.VISIBLE);
            ImageLoadUtil.displayImage(mContext, floorBean.getBanners().get(0).getImg(), img);
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String actionUrl = bannerBeanList.get(0).getActionUrl();
                    if (!TextUtils.isEmpty(actionUrl)) {
                        ToIntentUtil.toCommonIntent(bannerBeanList.get(0).getActionUrl(), mContext);
                    }
                }
            });
        } else {
            if(bannerBeanList!=null&&bannerBeanList.size()!=0){
                banner.setVisibility(View.VISIBLE);
                img.setVisibility(View.GONE);
                banner.setImages(bannerBeanList)
                        .setImageLoader(new GlideImageLoader())
                        .setDelayTime(5000)
                        .setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int i) {
                                String actionUrl = bannerBeanList.get(i).getActionUrl();
                                if(!TextUtils.isEmpty(actionUrl)){
                                    ToIntentUtil.toCommonIntent(bannerBeanList.get(i).getActionUrl(),mContext);
                                }

                            }
                        })
                        .start();
            }else{
                banner.setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onClick(BaseViewHolder helper, FloorBean data, int position) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, FloorBean data, int position) {
        return false;
    }
}
